package cn.xshi.sys.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.hutool.core.collection.CollectionUtil;
import cn.xshi.sys.form.SysNotifyReceiverForm;
import cn.xshi.sys.model.SysNotify;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.util.StringUtil;
import cn.xshi.sys.model.SysNotifyReceiver;
import cn.xshi.sys.service.SysNotifyReceiverService;
import cn.xshi.sys.service.SysNotifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
/**
 * @Desc 通知接收人
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/sysNotifyReceiver")
@Api(value = "通知接收人API",tags = "通知接收人API",description = "通知接收人API")
@Slf4j
public class SysNotifyReceiverController extends BaseAction {

	@Resource
	private SysNotifyReceiverService sysNotifyReceiverService;

	@Resource
	private SysNotifyService sysNotifyService;

	/**
	 * 查询通知接收人列表并分页
	 * @param sysNotifyReceiverForm
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@ApiOperation(value="查询通知接收人列表并分页", notes="查询通知接收人列表并分页")
	@GetMapping(value="/list")
	@Auth(value = "/sysNotifyReceiver/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BasePage<List<SysNotifyReceiver>> getSysNotifyReceiverListByCondition(SysNotifyReceiverForm sysNotifyReceiverForm, Integer pageNo, Integer pageSize){
		sysNotifyReceiverForm.setReceiveId(getCurrentAccountId());
		commonHPagerByPageNo(pageNo,pageSize);
		List<SysNotifyReceiver> sysNotifyReceiverList = sysNotifyReceiverService.getSysNotifyReceiverListByCondition(sysNotifyReceiverForm);
		if(CollectionUtil.isNotEmpty(sysNotifyReceiverList)){
			for(SysNotifyReceiver sysNotifyReceiver : sysNotifyReceiverList){
				if(StringUtil.isEmpty(sysNotifyReceiver.getFromUser())){
					SysNotify sysNotify = sysNotifyService.getSysNotifyById(sysNotifyReceiver.getNotifyId());
					if(null != sysNotify){
						OauthAccountEntity createBy = getAccount(sysNotify.getCreateId());
						if(null != createBy){
							sysNotifyReceiver.setFromUserName(createBy.getName());
						}
					}
				}
			}
		}
		PageInfo<SysNotifyReceiver> page = new PageInfo<SysNotifyReceiver>(sysNotifyReceiverList);
		return outPageBootByPageNo(page,pageNo,pageSize);
	}

	/**
	 * 查询单个我的通知
	 * @param id
	 */
	@ApiOperation(value="查询单个我的通知", notes="查询单个我的通知")
	@GetMapping(value="/get/{id}")
	@Auth(value = "/sysNotifyReceiver/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<SysNotifyReceiver> getSysNotifyReceiverById(@PathVariable("id")String id){
		SysNotifyReceiver sysNotifyReceiver = sysNotifyReceiverService.getSysNotifyReceiverById(id);
		if(StringUtil.isEmpty(sysNotifyReceiver.getFromUser())){
			SysNotify sysNotify = sysNotifyService.getSysNotifyById(sysNotifyReceiver.getNotifyId());
			if(null != sysNotify){
				OauthAccountEntity createBy = getAccount(sysNotify.getCreateId());
				if(null != createBy){
					sysNotifyReceiver.setFromUserName(createBy.getName());
				}
			}
		}
		return BaseResult.success(sysNotifyReceiver);
	}
	
	/**
	* 删除
	* @param id
	*/
	@DeleteMapping(value="/delete")
	@ApiOperation(value="删除通知接收人", notes="删除通知接收人")
	public BaseResult delSysNotifyReceiver(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i= sysNotifyReceiverService.delSysNotifyReceiver(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
}
